package net.sumcraft.mythicmobitemhandler.Commands;

import io.lumine.xikage.mythicmobs.MythicMobs;
import io.lumine.xikage.mythicmobs.adapters.bukkit.BukkitAdapter;
import io.lumine.xikage.mythicmobs.io.IOHandler;
import io.lumine.xikage.mythicmobs.io.IOLoader;
import io.lumine.xikage.mythicmobs.items.ItemManager;
import io.lumine.xikage.mythicmobs.items.MythicItem;
import net.md_5.bungee.api.ChatColor;
import net.sumcraft.mythicmobitemhandler.util.$;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import pw.yumc.YumCore.tellraw.Tellraw;

import java.io.File;
import java.util.List;

public class ItemSaverCommand implements CommandExecutor {

    private ItemManager itemManager = MythicMobs.inst().getItemManager();


    private IOLoader defaultItems = new IOLoader(MythicMobs.inst(), "ExampleItems.yml", "Items");
    private List<File> itemFiles = IOHandler.getAllFiles(defaultItems.getFile().getParent());


    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!sender.isOp()) {
            return false;
        }
        if (args.length == 0) {
            sender.sendMessage("§6/mmits get <物品名> §7获得指定道具");
            sender.sendMessage("§6/mmits give <物品名> <玩家名> §7给予指定玩家默认为1");
            sender.sendMessage("§6/mmits save <物品名> <文件名> §7将物品存到mm指定文件夹");
            sender.sendMessage("§6/mmits give <物品名> <数量> <玩家名> §7给予指定数量指定玩家");
            sender.sendMessage("§6/mmits give <物品名> <数量> <level> <玩家名> §7给予指定数量指定玩家,并替换物品的中的$level$");
            sender.sendMessage("§6/mmits drop <物品名> <数量> <level> <玩家名> §7在玩家位置掉落物品,并替换物品的中的$level$");
            sender.sendMessage("§6/mmits list §7列出所有物品");
            sender.sendMessage("§6/mmits list <文件名> §7列出目标文件中所有物品");
            sender.sendMessage("§6/mmits files §7列出所有物品文件名");
            return false;
        }

        //its list 命令
        if (args.length == 1) {
            if ("list".equalsIgnoreCase(args[0])) {
                Player player = (Player) sender;
                StringBuilder stringBuilder = new StringBuilder();
                for (String str : itemManager.getItemNames()
                        ) {
                    stringBuilder.append("§c").append(str).append("§7,");
                }
                player.sendMessage("§a共 §6" + itemManager.getItemNames().size() + " §a种物品§7:");
                player.sendMessage(stringBuilder.toString());
                return false;
            }
            if ("files".equalsIgnoreCase(args[0])) {
                Player player = (Player) sender;
                player.sendMessage("§c文件数量:" + itemFiles.size());
                String fileName = "";
                for (File f : itemFiles
                        ) {
                    fileName = fileName + ChatColor.GREEN + f.getName() + ChatColor.GRAY + ",";
                }
                player.sendMessage("§c文件列表:");
                player.sendMessage(fileName);
                return false;
            }
        }
        //its get [name] 会替换PAPI变量
        if (args.length == 2) {
            if ("get".equalsIgnoreCase(args[0])) {
                Player player = (Player) sender;
                if (!itemManager.getItemNames().contains(args[1])) {
                    player.sendMessage("该物品不存在");
                    return false;
                }
                ItemStack itemStack = itemManager.getItemStack(args[1]);
                $.replacePlaceholder(itemStack, player);
                $.randomLore(itemStack);
                $.replaceColreCode(itemStack);
                player.getInventory().addItem(itemStack);
                player.updateInventory();
                return false;
            }
            if ("list".equalsIgnoreCase(args[0])) {
                Player player = (Player) sender;
                Boolean hasFile = false;
                for (File f : itemFiles
                        ) {
                    if (f.getName().equals(args[1])) {
                        hasFile = true;
                        break;
                    }
                }
                if (!hasFile) {
                    player.sendMessage("文件 " + args[1] + " 不存在");
                    return false;
                }
                player.sendMessage("文件" + args[1] + "中的物品有:");
                //List<ItemStack> itemStacks = new ArrayList();
                for (MythicItem i : itemManager.getItems()
                        ) {
                    if (i.getFile().equalsIgnoreCase(args[1])) {
                        ItemStack itemStack = new ItemStack(BukkitAdapter.adapt(i.generateItemStack(1)));
                        $.replaceColreCode(itemStack);
                        //itemStacks.add(itemStack);
                        String cmd = "/mmits get " + i.getInternalName();
                        Tellraw tellraw = Tellraw.create();
                        tellraw.text(itemStack.getItemMeta().getDisplayName()).item(itemStack).command(cmd).send(player);
                    }
                }
                //for (ItemStack i : itemStacks
                //        ) {
                //    Tellraw tellraw = Tellraw.create();
                //    tellraw.text(i.getItemMeta().getDisplayName()).item(i).send(player);
                //}

            }
        }
        //its give [name] [player] 会替换PAPI变量
        if (args.length == 3) {
            if ("give".equalsIgnoreCase(args[0])) {
                Player p = Bukkit.getPlayer(args[2]);
                if (!itemManager.getItemNames().contains(args[1])) {
                    p.sendMessage("该物品不存在");
                    return false;
                }
                ItemStack itemStack = itemManager.getItemStack(args[1]);
                $.replacePlaceholder(itemStack, p);
                $.randomLore(itemStack);
                $.replaceColreCode(itemStack);
                p.getInventory().addItem(itemStack);
                p.updateInventory();
                return false;

            }
            // its save [name] [filename] 存入物品
            if ("save".equalsIgnoreCase(args[0])) {
                Player player = (Player) sender;
                if (itemManager.getItemNames().contains(args[1])) {
                    player.sendMessage("该物品已存在");
                    return false;
                }
                String cmd = "mythicmobs i import " + args[1] + " " + args[2];
                Bukkit.dispatchCommand(player, cmd);
            }
        }
        //its give [name] [amount] [player] 会替换PAPI变量
        if (args.length == 4) {
            if ("give".equalsIgnoreCase(args[0])) {
                Player p = Bukkit.getPlayer(args[3]);
                if (!itemManager.getItemNames().contains(args[1])) {
                    p.sendMessage("该物品不存在");
                    return false;
                }
                ItemStack itemStack = itemManager.getItemStack(args[1]);
                itemStack = $.replacePlaceholder(itemStack, p);
                itemStack.setAmount(Integer.parseInt(args[2]));
                $.randomLore(itemStack);
                $.replaceColreCode(itemStack);
                p.getInventory().addItem(itemStack);
                p.updateInventory();
                return false;
            }
        }
        //its give [name] [amount] [level] [player]  会替换PAPI变量,并且字符串中的$level$会被替换为level;
        if (args.length == 5) {
            if ("give".equalsIgnoreCase(args[0])) {

                Player p = Bukkit.getPlayer(args[4]);
                if (!itemManager.getItemNames().contains(args[1])) {
                    p.sendMessage("该物品不存在");
                    return false;
                }
                ItemStack itemStack = itemManager.getItemStack(args[1]);
                $.replaceLevel(itemStack, Integer.parseInt(args[3]));
                itemStack = $.replacePlaceholder(itemStack, p);
                $.randomLore(itemStack);
                $.replaceColreCode(itemStack);
                itemStack.setAmount(Integer.parseInt(args[2]));
                p.getInventory().addItem(itemStack);
                p.updateInventory();
                return false;
            }
            if ("drop".equalsIgnoreCase(args[0])) {

                Player p = Bukkit.getPlayer(args[4]);
                if (!itemManager.getItemNames().contains(args[1])) {
                    p.sendMessage("该物品不存在");
                    return false;
                }
                ItemStack itemStack = itemManager.getItemStack(args[1]);
                $.replaceLevel(itemStack, Integer.parseInt(args[3]));
                itemStack = $.replacePlaceholder(itemStack, p);
                $.randomLore(itemStack);
                $.replaceColreCode(itemStack);
                itemStack.setAmount(Integer.parseInt(args[2]));
                p.getWorld().dropItem(p.getLocation().add(0, 1, 0), itemStack);
                return false;
            }
        }

        return false;
    }

}
